import 'package:ycbl/model/free_daily_list_entity.dart';
import 'package:ycbl/net/repository/bl_repository.dart';
import 'package:ycbl/widget/provider/base_view_state_model.dart';

/// （免费）每日精品直播
class FreeDailyLiveViewModel extends BaseViewStateModel {

  Map<DateTime, List<FreeDailyLiveEntity>> _dailyLiveMap = {};

  /// 按日期分组的直播
  Map<DateTime, List<FreeDailyLiveEntity>> get dailyLiveMap => _dailyLiveMap;

  int successTime = 0;

  ///获取某段时间内的精品直播
  getDailyLives(DateTime startDate, DateTime endDate) {

    BLRepository.getDailyLives(startDate, endDate, (list) {
      _groupByDate(list);
      successTime ++;
      setIdle();
    }, (error) {
      setError(error);
    });

  }

  ///按日期分组
  _groupByDate(List<FreeDailyLiveEntity> list) {
    // 清空集合
    _dailyLiveMap.clear();
    if (list.isEmpty) {
      return;
    }
    //按日期分组 yyyy-MM-dd 00:00:000
    _dailyLiveMap = Map.fromIterable(
      list,
      key: (key) => key.groupDate,
      value: (value) {
        return list.where((element) => element.groupDate == value.groupDate).toList();
      },
    );

  }

  ///获取某一天的直播
  List<FreeDailyLiveEntity> getLivesAtDay(DateTime date) {
    DateTime key = DateTime(date.year, date.month, date.day);
    return _dailyLiveMap[key] ?? [];
  }

}